//前台调用
var $ =function(){
    return new Base();  //每次调用Base都会去new出一个新的对象
}

//基础库
function Base(){
    //创建数组，来保存元素节点或节点数组
    //Base.prototype.elements = [];   //把Base.prototype改为this,new出对象时设置为私有化，这样前面的样式不会被后面的覆盖
    this.elements = [];
}




//获取ID节点
Base.prototype.getId = function(id){
    this.elements.push(document.getElementById(id));
    return this;

}

//获取元素节点
Base.prototype.getTagName = function(tag){
    var tags = document.getElementsByTagName(tag);
    for(var i=0;i<tags.length;i++){
        this.elements.push(tags[i]);
    }
    return this;
}



//获取Class数组节点
Base.prototype.getByClass = function(className,idName){
    var node = null;
    if(arguments.length == 2){
        node = document.getElementById(idName);
    }else{
        node = document; //文档所有节点的父节点
    }

    var all = node.getElementsByTagName('*');
    for(var i=0;i<all.length;i++){
        if(all[i].className == className){
            this.elements.push(all[i]);
        }
    }

    return this;

}
   



//获取某一个节点
Base.prototype.getElement = function (num) {    
    var element = this.elements[num];
    this.elements = [];
    this.elements[0] = element;
    return this;
};



//添加Class
Base.prototype.addClass = function(className){
    for(var i=0;i<this.elements.length;i++){
        //正则匹配  出现重复的不会再添加  只添加一次
        if(!this.elements[i].className.match(new RegExp('(\\s|^)'+className+'(\\s|$)'))){
            this.elements[i].className += ' '+className;   //添加多个class,用 += ，' '表示字符串之间空格隔开
        }
    }
    return this;
}


//移除Class
Base.prototype.removeClass = function (className) {
    for (var i = 0; i < this.elements.length; i ++) {
        if (this.elements[i].className.match(new RegExp('(\\s|^)' +className +'(\\s|$)'))) {
            this.elements[i].className = this.elements[i].className.replace(new RegExp('(\\s|^)' +className +'(\\s|$)'), ' ');
        }
    }
    return this;
}

//移出Class
Base.prototype.removeClass = function (className){
    for(var i=0;i<this.elements.length;i++){
        if(this.elements[i].className.match(new RegExp('(\\s|^)' + '(\\s|$)'))){
            //移出class,就是用空字符串替代原有的class
            this.elements[i].className = this.elements[i].className.replace(new RegExp('(\\s|^)'+className+'(\\s|$)'),' ');  
        }
    }
}



//添加link或style的CSS规则
Base.prototype.addRule = function (num, selectorText, cssText, position) {
    var sheet = document.styleSheets[num];
    if (typeof sheet.insertRule != 'undefined') {//W3C
        sheet.insertRule(selectorText + '{' + cssText + '}', position);
    } else if (typeof sheet.addRule != 'undefined') {//IE
        sheet.addRule(selectorText, cssText, position);
    }
    return this;
}

//移除link或style的CSS规则
Base.prototype.removeRule = function (num, index) {
    var sheet = document.styleSheets[num];
    if (typeof sheet.deleteRule != 'undefined') {//W3C
        sheet.deleteRule(index);
    } else if (typeof sheet.removeRule != 'undefined') {//IE
        sheet.removeRule(index);
    }
    return this;
}



//设置鼠标移入移出方法
Base.prototype.hover = function(over,out){
    for(var i=0;i<this.elements.length;i++){
        this.elements[i].onmouseover = over;   //鼠标移出执行over方法
        this.elements[i].onmouseout = out ;    //鼠标移出执行out方法
    }
    return this;
}

//设置显示
Base.prototype.show = function(){
    for(var i=0;i<this.elements.length;i++){
        this.elements[i].style.display = 'block';
    }

    return this;
}


//设置隐藏
Base.prototype.hide= function(){
    for(var i=0;i<this.elements.length;i++){
        this.elements[i].style.display = 'none';
    }

    return this;
}


//设置登录框居中
Base.prototype.center = function(width,height){
   
    var top = (document.documentElement.clientHeight-252)/2;
    var left = (document.documentElement.clientWidth-502)/2;

    for(var i=0;i<this.elements.length;i++){
        this.elements[i].style.top = top + 'px';
        this.elements[i].style.left = left + 'px';
    }

    return this;
}


//触发浏览器窗口事件
// Base.prototype.resize = function(fn){
//     window.onresize = fn;
//     return this;
// }

//触发浏览器窗口事件
Base.prototype.resize = function (fn) {
    for (var i = 0; i < this.elements.length; i ++) {
        var element = this.elements[i];
        window.onresize = function () {
            fn();
            if (element.offsetLeft > getInner().width - element.offsetWidth) {
                element.style.left = getInner().width - element.offsetWidth + 'px';
            }
            if (element.offsetTop > getInner().height - element.offsetHeight) {
                element.style.top = getInner().height - element.offsetHeight + 'px';
            }
        };
    }
    return this;
}


//跨浏览器获取视口大小
function getInner() {
    if (typeof window.innerWidth != 'undefined') {
        return {
            width : window.innerWidth,
            height : window.innerHeight
        }
    } else {
        return {
            width : document.documentElement.clientWidth,
            height : document.documentElement.clientHeight
        }
    }
}

//触发点击事件
Base.prototype.click = function(fn){
    for(var i=0;i<this.elements.length;i++){
        this.elements[i].onclick = fn;
    }

    return this;
}


//锁屏功能
Base.prototype.lock = function(){
    for(var i=0;i<this.elements.length;i++){
        //自动获取浏览器窗口的宽高
        this.elements[i].style.width = document.documentElement.clientWidth + 'px';
        this.elements[i].style.height = document.documentElement.clientHeight + 'px';
        this.elements[i].style.display = 'block';
        //当我在拖拽的时候，禁止滚动条
        document.documentElement.style.overflow = 'hidden';
    }

    return this;
}

//解锁功能  取消锁屏
Base.prototype.unlock = function(){
    for(var i=0;i<this.elements.length;i++){
        this.elements[i].style.display = 'none';
        //当我关闭拖动时，还原默认滚动条状态
        document.documentElement.style.overflow = 'auto';    //这样做的目的是避免我拖拽的时候有滚动条，出现白边，应该控制在可视区内
    }
    return this;
}



//获取event对象
function getEvent(event){
    return event || window.event;
}


//拖拽功能
// Base.prototype.drag = function(){
//     var oDiv = document.getElementById('login'); //获取被拖拽元素
//     for(var i=0;i<this.elements.length;i++){
//         this.elements[i].onmousedown = function(e){      //点击oDiv
       
//             //调用getEvent获取event对象
//             var e = getEvent(e);
//             //求鼠标点击的点到拖拽窗口的横纵坐标
//             var diffX = e.clientX - oDiv.offsetLeft;  
//             var diffY = e.clientY - oDiv.offsetTop;


//              //alert(getInner().Width);    //undefined?????
//             //移动oDiv,在整个窗口区域，所以要document获取
//             document.onmousemove = function(e){    //传个参数  event对象
//                 var e = getEvent(e);
//                 //动态求出可视区边框到拖拽窗口点的横纵坐标
//                 var left =  e.clientX - diffX ;
//                 var top = e.clientY - diffY;


//                 //限制拖拽只能在浏览器可视窗口
//                 //做判断
//                 if(left<0){
//                     left = 0;
//                 }else if(left>getInner().width-oDiv.offsetWidth){
//                     left = getInner().width-oDiv.offsetWidth;
//                 }

//                 if(top<0){
//                     top = 0;
//                 }else if(top>getInner().height-oDiv.offsetHeight){
                   
//                    top = getInner().height-oDiv.offsetHeight;
//                 }

//                 oDiv.style.left = left + 'px';
//                 oDiv.style.top = top + 'px';
//             }

//             //停止
//             document.onmouseup = function(){
//                 document.onmousemove = null;
//                 document.onmouseup = null;
//             }

//         }

//     }

//     return this;
// }


//拖拽功能
Base.prototype.drag = function () {
    for (var i = 0; i < this.elements.length; i ++) {
        addEvent(this.elements[i], 'mousedown', function (e) {
            if (trim(this.innerHTML).length == 0) e.preventDefault();
            var _this = this;
            var diffX = e.clientX - _this.offsetLeft;
            var diffY = e.clientY - _this.offsetTop;
            
            if (e.target.tagName == 'H2') {
                addEvent(document, 'mousemove', move);
                addEvent(document, 'mouseup', up);
            } else {
                removeEvent(document, 'mousemove', move);
                removeEvent(document, 'mouseup', up);
            }
            
            function move(e) {
                var left = e.clientX - diffX;
                var top = e.clientY - diffY;
                
                if (left < 0) {
                    left = 0;
                } else if (left > getInner().width - _this.offsetWidth) {
                    left = getInner().width - _this.offsetWidth;
                }
                
                if (top < 0) {
                    top = 0;
                } else if (top > getInner().height - _this.offsetHeight) {
                    top = getInner().height - _this.offsetHeight;
                }
                
                _this.style.left = left + 'px';
                _this.style.top = top + 'px';
                
                if (typeof _this.setCapture != 'undefined') {
                    _this.setCapture();
                } 
            }
            
            function up() {
                removeEvent(document, 'mousemove', move);
                removeEvent(document, 'mouseup', up);
                if (typeof _this.releaseCapture != 'undefined') {
                    _this.releaseCapture();
                }
            }
        });
    }
    return this;
}



//设置CSS
Base.prototype.css = function(attr,value){
    for(var i=0;i<this.elements.length;i++){
        if(arguments.length == 1){
            if(typeof window.getComputedStyle != 'undefined'){
                return window.getComputedStyle(this.element[i],null)[attr];  //W3C
            }else if(typeof this.element[i].currentStyle != 'undefined'){
                return this.element[i].currentStyle[attr];  //IE
            }
        }
        this.elements[i].style[attr] = value;
    }
    
    return this;
}

//设置innerHTML
Base.prototype.html = function(str){
    for(var i=0;i<this.elements.length;i++){
        if(arguments.length == 0){
            this.elements[i].innerHTML;
        }
        this.elements[i].innerHTML = str;
    }
    return this;
}


//阻止默认行为
function preDef(event) {
    var e = getEvent(event);
    if (typeof e.preventDefault != 'undefined') {//W3C
        e.preventDefault();
    } else {//IE
        e.returnValue = false;
    }
}